// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.Lua.LuaEventManager;
import zombie.ai.states.SwipeStatePlayer;
import zombie.characterTextures.BloodBodyPartType;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoLivingCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.inventory.types.HandWeapon;
import zombie.network.packets.INetworkPacket;

public class WeaponHit extends Hit implements INetworkPacket {
    protected float range;
    protected boolean hitHead;

    public void set(boolean ignore, float damage, float _range, float hitForce, float hitDirectionX, float hitDirectionY, boolean _hitHead) {
        super.set(ignore, damage, hitForce, hitDirectionX, hitDirectionY);
        this.range = _range;
        this.hitHead = _hitHead;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        this.range = b.getFloat();
        this.hitHead = b.get() != 0;
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        b.putFloat(this.range);
        b.putBoolean(this.hitHead);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + "\n\tWeapon [ range=" + this.range + " | hitHead=" + this.hitHead + " ]";
    }

    void process(IsoGameCharacter isoGameCharacter1, IsoGameCharacter isoGameCharacter0, HandWeapon handWeapon) {
        isoGameCharacter0.Hit(handWeapon, isoGameCharacter1, this.damage, this.ignore, this.range, true);
        super.process(isoGameCharacter1, isoGameCharacter0);
        LuaEventManager.triggerEvent("OnWeaponHitXp", isoGameCharacter1, handWeapon, isoGameCharacter0, this.damage);
        if (isoGameCharacter1.isAimAtFloor() && !handWeapon.isRanged() && isoGameCharacter1.isNPC()) {
            SwipeStatePlayer.splash(isoGameCharacter0, handWeapon, isoGameCharacter1);
        }

        if (this.hitHead) {
            SwipeStatePlayer.splash(isoGameCharacter0, handWeapon, isoGameCharacter1);
            SwipeStatePlayer.splash(isoGameCharacter0, handWeapon, isoGameCharacter1);
            isoGameCharacter0.addBlood(BloodBodyPartType.Head, true, true, true);
            isoGameCharacter0.addBlood(BloodBodyPartType.Torso_Upper, true, false, false);
            isoGameCharacter0.addBlood(BloodBodyPartType.UpperArm_L, true, false, false);
            isoGameCharacter0.addBlood(BloodBodyPartType.UpperArm_R, true, false, false);
        }

        if ((!((IsoLivingCharacter)isoGameCharacter1).bDoShove || isoGameCharacter1.isAimAtFloor())
            && isoGameCharacter1.DistToSquared(isoGameCharacter0) < 2.0F
            && Math.abs(isoGameCharacter1.z - isoGameCharacter0.z) < 0.5F) {
            isoGameCharacter1.addBlood(null, false, false, false);
        }

        if (!isoGameCharacter0.isDead()
            && !(isoGameCharacter0 instanceof IsoPlayer)
            && (!((IsoLivingCharacter)isoGameCharacter1).bDoShove || isoGameCharacter1.isAimAtFloor())) {
            SwipeStatePlayer.splash(isoGameCharacter0, handWeapon, isoGameCharacter1);
        }
    }
}
